package ReSofting.it.GoldDirector.Helpers;

import java.net.SocketException;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.apache.http.util.EntityUtils;
import org.json.JSONArray;
import org.json.JSONObject;
import android.content.Context;
import android.os.AsyncTask;
import android.util.Log;

import ReSofting.it.GoldDirector.Interfaces.IRemoteCallListener;
import ReSofting.it.GoldDirector.JsonTrans.JsonTransformer_myOrdine;
import ReSofting.it.GoldDirector.Model.ReturnData;
import ReSofting.it.GoldDirector.Model.myOrdine;

public class GetOrdersAsync  extends AsyncTask<String, Void, ReturnData< List<myOrdine>>> {

	 
    //private final static String TAG = GetDataAsync.class.getName();
    
    IRemoteCallListener<ReturnData< List<myOrdine>>> listener;
    Context _context= null;
    String _responseString = null;
    HttpResponse _httpResponse = null;
    
    public GetOrdersAsync(IRemoteCallListener<ReturnData< List<myOrdine>>> listener, Context pContext) {
        this.listener = listener;
        _context = pContext;
    }
 
    @Override
    protected ReturnData< List<myOrdine>> doInBackground(String... urls) 
    {
    	//HttpEntity entity = null;
        List<myOrdine> oggettiList = new ArrayList<myOrdine>();
        ReturnData< List<myOrdine>>  _retData = null ;
        String url = urls[0];
      
        HttpParams httpParameters = new BasicHttpParams();
	     HttpConnectionParams.setConnectionTimeout(httpParameters, 30000);
	     HttpConnectionParams.setSoTimeout(httpParameters, 30000);
               

        DefaultHttpClient httpClient = new DefaultHttpClient();
        httpClient.setParams(httpParameters);

        //String url2 = "http://192.168.1.2:8096/Services/OrdersSvc.svc/GetCategorie";
        
        HttpGet request = new HttpGet(url); //8096	 
        request.setHeader("Accept", "application/json");
        request.setHeader("Content-type", "application/json");

        try {
        	        	
        	_httpResponse = httpClient.execute(request);     
             
             if(_httpResponse != null)
                 _responseString =   EntityUtils.toString(_httpResponse.getEntity());      
                 
                if(_httpResponse == null | _responseString == null)
                {
                	 _retData = new ReturnData< List<myOrdine> >  (
                			 9000, null,true,"response data is null", null);
                	 
                	 return _retData; //return
                }
                else 
                {
                	 _retData = new ReturnData< List<myOrdine> >  (
                			 _httpResponse.getStatusLine().getStatusCode(),
                			 _httpResponse.getAllHeaders(), false,"", oggettiList  );
                }
              
                 
                if(_responseString == null)
                	return _retData;
                
  
                 JSONArray rawPersonaList = new JSONArray(_responseString);
                                                   
                 myOrdine dato = null ;
                 JSONObject jsObj = null;
                 JsonTransformer_myOrdine jsonner = new JsonTransformer_myOrdine();
                 
                 for(int i = 0; i < rawPersonaList.length(); i++) {

                     jsObj = rawPersonaList.getJSONObject(i);
                     dato = jsonner.jsonTo_myOrdine(jsObj);
                	 
                 	 oggettiList.add(dato);
                 }
   
             //oggettiList.add("\r\n SC:"+ response.getStatusLine());
        }
                 catch(SocketException ex)
                 {
                   Log.e("Error in GetOrdersAsync : " , "Error on soapPrimitiveData() " + ex.getMessage());
                     ex.printStackTrace();                    
                 }             
		         catch (Exception e) {
		         		            
		            Log.e("Error in GetOrdersAsync : " , "Error on soapPrimitiveData() " + e.getMessage());		            
		            e.printStackTrace();  		            
		        }
       
        
         	return _retData;
    }
 
    protected void onPostExecute(ReturnData< List<myOrdine>> retData) {
       listener.onRemoteCallComplete(retData);
    }
 
}
